package com.codi.bus.util;

import java.util.Random;
import java.util.UUID;

public class GeneratorUtil {
    private static final String[] RANDOMS = new String[]{"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j"
            ,"k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

    private static final String[] CHAR_RANDOMS = new String[]{"a","b","c","d","e","f","g","h","i","j"
            ,"k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};

    /**
     *
     * @描述：首字母大写
     * @参数：@param name
     * @参数：@return
     * @作者：曾祥亮
     * @时间：2017年6月26日 上午9:15:08
     */
    public static String captureName(String name) {
        char[] cs=name.toCharArray();
        if(Character.isDigit(cs[0])||(cs[0] >='A' && cs[0] <='Z')){
            return "Zengxl"+String.valueOf(cs);
        }else{
            cs[0]-=32;
            return String.valueOf(cs);
        }
    }
    /**
     *
     * @描述：32位UUID字符串
     * @参数：@return
     * @作者：曾祥亮
     * @时间：2016年12月14日 下午2:12:21
     */
    public static String getUUID(){
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString().replace("-", "");
        uuidStr = uuidStr.toUpperCase();
        return uuidStr;
    }
    /**
     *
     * @描述：生成size位数随机
     * @参数：@return
     * @作者：曾祥亮
     * @时间：2016年12月14日 下午2:25:00
     */
    public static String getRandomNum(int size){
        StringBuffer sb = new StringBuffer();
        Random r = new Random();
        sb.append(CHAR_RANDOMS[r.nextInt(CHAR_RANDOMS.length)]);
        for(int i=1;i<size;i++){
            sb.append(RANDOMS[r.nextInt(RANDOMS.length)]);
        }
        return sb.toString();
    }

    /**
     *
     * @描述： 生成size位随机整数
     * @参数：@return
     * @作者：任英杰
     * @时间：2017年4月18日下午8:55:06
     */
    public static String getRandomDigits(int size){
        StringBuffer sb = new StringBuffer();
        Random r = new Random();
        for(int i = 0; i < size; i++){
            sb.append(r.nextInt(10));
        }
        return sb.toString();
    }

    /**
     *
     * @param name 类属性名
     * @return
     * @throws Exception 通过类属性名获得该属性的set方法
     * @description
     * @version 1.0
     * @author zengxl
     * @update 2012-12-28 下午05:15:23
     */
    public static String getSetMethodName(String name) throws Exception
    {
        if (name == null || name.trim().length() == 0)
        {
            throw new IllegalArgumentException("Name is not specified!");
        }

        String upperCaseVarName = name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toUpperCase());

        return "set" + upperCaseVarName;
    }

    /**
     *
     * @param name 类属性名
     * @return
     * @throws Exception 通过类属性名获得该属性的get方法
     * @description
     * @version 1.0
     * @author zengxl
     * @update 2012-12-28 下午05:15:23
     */
    public static String getGetMethodName(String name)throws Exception
    {
        if (name == null || name.trim().length() == 0)
        {
            throw new IllegalArgumentException("Name is not specified!");
        }

        String upperCaseVarName = name.replaceFirst(name.substring(0, 1),name.substring(0, 1).toUpperCase());

        return "get" + upperCaseVarName;
    }
    /**
     *
     * @param tableName 表名
     * @return
     * @description 取得实体名称
     * @version 1.0
     * @author zengxl
     * @update 2013-1-7 上午10:14:40
     */
    public static String getEntityName(String tableName){
        String table = tableName.toLowerCase();
        char[] chars = table.toCharArray();
        int i = 0;
        StringBuffer sb = new StringBuffer("");
        execName(chars, i, sb);
        return sb.toString();
    }
    /**
     *
     * @描述：获取
     * @参数：@param propertyName
     * @参数：@return
     * @作者：曾祥亮
     * @时间：2016年12月20日 下午5:35:19
     */
    public static String getEntityPropertyName(String dbName){
        String propertyName = dbName.toLowerCase();
        char[] chars = propertyName.toCharArray();
        int i = 1;
        StringBuffer sb = new StringBuffer("");
        execName(chars, i, sb);
        return sb.toString();
    }

    private static void execName(char[] chars, int i, StringBuffer sb) {
        for(char c:chars){
            String temp = String.valueOf(c);
            if("_".equals(temp)){
                i = 0;
                continue;
            }
            if(i == 0){
                sb.append(temp.toUpperCase());
            }else{
                sb.append(temp);
            }
            i++;
        }
    }

    /**
     *
     * @描述：通过属性名字获取数据库对应字段名字
     * @参数：@param entityPropertyName
     * @参数：@return
     * @作者：曾祥亮
     * @时间：2017年5月5日 上午9:44:21
     */
    public static String getDbName(String propertyName){
        String entityPropertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1);
        char[] chars = entityPropertyName.toCharArray();
        StringBuffer sb = new StringBuffer("");
        for(char c:chars){
            //判断是否为数字
            if(Character.isLowerCase(c) || Character.isDigit(c)){
                sb.append(c);
            }else{
                sb.append("_").append(String.valueOf(c).toLowerCase());
            }
        }
        return sb.toString();
    }
}
